<template>
  <div class="receiver" ref="receiverbox" @scroll="addData">
    <div class="receiver-top">
      <div @click="show = true">
        <span>{{ year }}年-0{{ month }}月</span><van-icon name="arrow-down" />
      </div>
    </div>
    <div class="receiver-box">
      <div class="recList" v-for="item in data" :key="item.id">
        <van-divider v-if="item.top" @click.stop>{{ item.top }}月</van-divider>
        <div class="titles" v-if="item.id" @click="goRadio(item.id)">
          <div class="radioTop">
            <img :src="item.cover" alt="" />
            <div class="radiomin">
              <div>
                <span>ONE</span>
                <img src="@/assets/硬件_收音机1_1.png" alt="" /><span
                  style="font-size: 12px; margin-left: 10px"
                  >{{ item.volume }}</span
                >
              </div>
            </div>
            <div class="radTit">
              {{ item.title }}
            </div>
          </div>
          <div class="radioBottom">
            <div class="botleft" v-if="item.author">
              <div class="botimg">
                <img :src="item.author.web_url" alt="" /><span>{{
                  item.author.user_name
                }}</span>
              </div>
            </div>
            <div class="botRight">
              <van-icon name="like-o" /><span
                style="font-size: 12px; margin-right: 20px"
                >{{ item.like_count }}</span
              >
              <img src="@/assets/分享.png" alt="" />
            </div>
          </div>
        </div>
      </div>
    </div>
    <!-- 时间选择器and遮罩层 -->
    <van-overlay :show="show" @click="show = false">
      <div class="wrapper" @click.stop>
        <div class="block">
          <van-datetime-picker
            v-model="currentDate"
            type="year-month"
            title="选择年月"
            :min-date="minDate"
            :max-date="maxDate"
            :formatter="formatter"
            @cancel="cancel()"
            @confirm="confirm()"
            @change="changeDate()"
          />
        </div>
      </div>
    </van-overlay>
  </div>
</template>

<script>
import { getRead } from "@/api/find";
export default {
  name: "receiver",
  data() {
    return {
      year: new Date().getFullYear(),
      month: new Date().getMonth() + 1,
      minDate: new Date(2016, 0, 1),
      maxDate: new Date(new Date().getFullYear(), new Date().getMonth(), 1),
      currentDate: new Date(),
      show: false,
      data: [],
      topmounth: null,
      oldYear: 0,
      oldMonth: 0,
      timer: null,
    };
  },
  created() {
    this.getReceiver();
  },
  methods: {
    formatter(type, val) {
      if (type === "year") {
        return `${val}年`;
      } else if (type === "month") {
        return `${val}月`;
      }
      return val;
    },
    confirm() {
      console.log("点击确认日期");
      console.log(this.currentDate.getFullYear());
      console.log(this.currentDate.getMonth() + 1);
      this.year = this.currentDate.getFullYear();
      this.month = this.currentDate.getMonth() + 1;
      this.$nextTick(() => {
        this.getReceiver();
      });

      this.show = false;
    },
    cancel() {
      console.log("点击了取消选择日期");
      this.show = false;
    },
    changeDate() {
      console.log("选项改变了");
    },
    async getReceiver() {
      const { data } = await getRead(
        8,
        this.year + "-" + (this.month < 10 ? "0" + this.month : this.month)
      );
      this.data = data;
      console.log(
        "获取收音机的数据",
        this.data,
        this.data[0].maketime.substr(5, 2)
      );
      // 在头部插入一个月份，代表当月一组数据
      this.data.unshift({ top: this.data[0].maketime.substr(5, 2) });
      console.log("获取收音机的数据", data);
    },
    addData() {
      //scrollTop就是触发滚轮事件时滚轮的高度
      // let scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
      let scrollTop = this.$refs.receiverbox.scrollTop;
      let scrollHeight = this.$refs.receiverbox.scrollHeight; //滚动条的高度
      let clientHeight = this.$refs.receiverbox.clientHeight; //浏览器的可视高度
      console.log("scrollTop", scrollTop);
      console.log(
        "滚动条高度",
        scrollHeight,
        "浏览器可视高度",
        scrollTop + clientHeight + 100
      );
      if (scrollTop + clientHeight >= scrollHeight) {
        if (this.timer) {
          clearTimeout(this.timer);
        }
        this.timer = setTimeout(() => {
          this.getLoading();
        }, 1000);
        console.log("触底");
      }
    },
    async getLoading() {
      // 这里判断截取的月份如果小于2 则oldmonth为12
      if (parseInt(this.data[this.data.length - 1].maketime.substr(5, 2)) < 2) {
        this.oldYear =
          parseInt(this.data[this.data.length - 1].maketime.substr(0, 4)) - 1;
        this.oldMonth = "12";
      } else {
        this.oldYear = parseInt(
          this.data[this.data.length - 1].maketime.substr(0, 4)
        );
        this.oldMonth =
          // 截取的月份小于10的话，要在前面添个0，否则就不用 用三目运算符实现
          parseInt(this.data[this.data.length - 1].maketime.substr(5, 2)) - 1 <
          10
            ? "0" +
              (parseInt(this.data[this.data.length - 1].maketime.substr(5, 2)) -
                1)
            : parseInt(this.data[this.data.length - 1].maketime.substr(5, 2)) -
              1;
      }
      const { data } = await getRead(8, this.oldYear + "-" + this.oldMonth);
      console.log("年月份", this.oldYear, this.oldMonth);
      console.log(
        "裁剪的数据",
        parseInt(this.data[this.data.length - 1].maketime.substr(5, 2)) - 1
      );
      // 每组数据前添加个月份top，用来展示
      this.data.push({ top: data[data.length - 1].maketime.substr(5, 2) });
      this.data.push(...data);
      console.log("新数据", this.data);
    },
    goRadio(id) {
      // 跳转到收音机页面
      console.log("跳转到收音机页面");
      this.$router.push({
        path: "/radio",
        query: {
          id: id,
        },
      });
    },
  },
};
</script>

<style lang="less" scoped>
.wrapper {
  .block {
    margin-top: 30px;
  }
}
.receiver {
  position: relative;
  width: 375px;
  background: #fcfcfc;
  max-height: 667px;
  overflow: scroll;
  .receiver-top {
    position: fixed;
    top: 0;
    text-align: center;
    width: 375px;
    height: 30px;
    background: #fcfcfc;
    z-index: 10;
  }
  .receiver-box {
    margin-top: 30px;
    width: 100%;
    padding: 0 15px;
    .recList {
      margin-bottom: 20px;
      .titles {
        .radioTop {
          position: relative;
          img {
            width: 100%;
            display: block;
            border-radius: 10px;
          }
          .radiomin {
            position: absolute;
            top: 10px;
            left: 10px;
            color: #ffffff;
            & > :nth-child(1) {
              display: flex;
              align-items: center;
              img {
                width: 18px;
                height: 18px;
              }
            }
          }
          .radTit {
            position: absolute;
            left: 10px;
            bottom: 10px;
            color: #ffffff;
            font-size: 14px;
          }
        }
        .radioBottom {
          padding: 10px;
          display: flex;
          justify-content: space-between;

          .botleft {
            .botimg {
              display: flex;
              align-items: center;
              img {
                width: 20px;
                height: 20px;
                border-radius: 50%;
              }
              span {
                font-size: 14px;
                color: #bdbdbd;
                margin-left: 5px;
              }
            }
          }
          .botRight {
            font-size: 20px;
            display: flex;
            align-items: center;
            color: #a7a7a7;
            img {
              width: 20px;
              height: 20px;
            }
          }
        }
      }
    }
  }
}
</style>